﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AD.Model;
using AD.BLL;

public partial class Manager_User_PageData_ShopLists : WebManagerForm
{
    public List<NetShopMod> Shops = new List<NetShopMod>();
    public int currPageIndex = 1;
    public int MaxSize = 0;
    public int MaxPage = 0;
    public string userName = "";
    public string keyWords = "";
    public string adTitle = "";
    public string endTime = "";
    public string startTime = "";
    public string SearchEngineType = "-1";
    public string Status = "-1";
    protected void Page_Load(object sender, EventArgs e)
    {
        string indexstr = Request.QueryString["pageIndex"];
        string action = Request.QueryString["action"];
        string idstr = Request.QueryString["id"];
        string idstrs = Request.Params["ids"];
        userName = Request.Params["userName"];
        keyWords = Request.Params["keyWords"];
        adTitle = Request.Params["adTitle"];
        endTime = Request.Params["endTime"];
        startTime = Request.Params["startTime"];
        SearchEngineType = Request.Params["SearchEngineType"];
        Status = Request.Params["Status"];
        string filter = " NetShopType=" + SearchEngineType;
        if (string.IsNullOrEmpty(SearchEngineType))
        {
            filter = "";
            SearchEngineType = "0";
        }
        else
        {
            if (SearchEngineType.Equals("0"))
                filter = "";
        }
        if (!string.IsNullOrEmpty(Status) && !Status.Equals("-1"))
        {
            if (!string.IsNullOrEmpty(filter))
                filter += " and Status=" + Status;
            else filter += " Status=" + Status;
        }
        else
            Status = "-1";

        if (!string.IsNullOrEmpty(keyWords) && !string.IsNullOrEmpty(filter))
            filter += string.Format(" and KeyWords like '%{0}%' ", keyWords);

        else if (!string.IsNullOrEmpty(keyWords) && string.IsNullOrEmpty(filter))
            filter += string.Format(" KeyWords like '%{0}%' ", keyWords);

        if (!string.IsNullOrEmpty(adTitle))
            filter += string.Format(" and AdTitle like '%{0}%' ", adTitle);

        if (!string.IsNullOrEmpty(startTime))
            filter += string.Format(" and Commit_Date >= '%{0}%' ", startTime);

        if (!string.IsNullOrEmpty(endTime))
            filter += string.Format(" and Commit_Date<='{0}' ", endTime);

        if (!string.IsNullOrEmpty(action))
        {
            bool effect = int.TryParse(indexstr, out currPageIndex);
            int id = 0;
            int.TryParse(idstr, out id);
            if (action.Equals("queryFilter") && effect == true)
            {
                SetDataBind(userName, filter, UtilHepler.PAGE_SIZE, 1);
            }
            else if (action.Equals("delete") && id > 0)
            {
                NetShopsBLL.Delete(id);
                SetDataBind(userName, filter, UtilHepler.PAGE_SIZE, currPageIndex);
            }
            else if (action.Equals("deleteAll"))
            {
                List<int> ids = GetIdToList(idstrs);
                foreach (var tmpId in ids)
                {
                    NetShopsBLL.Delete(tmpId);
                }
                SetDataBind(userName, filter, UtilHepler.PAGE_SIZE, 1);

            }
            else if (action.Equals("gopage") && effect == true)
            {
                SetDataBind(userName, filter, UtilHepler.PAGE_SIZE, currPageIndex);
            }
            else if (action.Equals("status"))
            {
                KeyWordsStatus status = (KeyWordsStatus)int.Parse(Request.QueryString["params"]);
                NetShopMod keywords = NetShopsBLL.QueryById(idstr);
                if (keywords != null)
                {
                    NetShopsBLL.UpdateAudit(status, keywords.AutoID);
                    SetDataBind(userName, filter, UtilHepler.PAGE_SIZE, currPageIndex);
                }
            }
            else if (action.Equals("pass"))
            {
                NetShopMod keywords = NetShopsBLL.QueryById(idstr);
                if (keywords != null)
                {
                    NetShopsBLL.UpdateAudit(keywords.Status == KeyWordsStatus.Pass ? KeyWordsStatus.Default : KeyWordsStatus.Pass, keywords.AutoID);
                    SetDataBind(userName, filter, UtilHepler.PAGE_SIZE, currPageIndex);
                }
            }
            //else if (action.Equals("status") && id > 0)
            //{
            //    NetShopMod keywords = NetShopsBLL.QueryById(idstr);
            //    if (keywords != null)
            //    {
            //        //NetShopsBLL.UpdateAudit(!keywords.Status, keywords.AutoID);
            //        SetDataBind(userName, filter, UtilHepler.PAGE_SIZE, currPageIndex);
            //    }
            //}
        }
    }

    void SetDataBind(string userName, string filter, int pageSize, int index)
    {
        if (!string.IsNullOrEmpty(userName))
        {
            List<UserMod> users = UserBLL.QueryFilterToList(" Email like '%" + userName + "%'");
            if (users != null)
            {
                foreach (UserMod user in users)
                {
                    string tmpFilter = filter + " and UserId=" + user.AutoID;
                    if (string.IsNullOrEmpty(filter))
                        tmpFilter = " UserId=" + user.AutoID;
                    List<NetShopMod> tmp = NetShopsBLL.QueryByPage(tmpFilter, pageSize, index == 0 ? 0 : index - 1, out MaxSize);
                    if (tmp != null && tmp.Count > 0)
                    {
                        Shops.AddRange(tmp);
                        if (Shops.Count > pageSize)
                            Shops.RemoveRange(pageSize, Shops.Count - pageSize);
                    }
                }
            }
        }
        else
        {
            Shops = NetShopsBLL.QueryByPage(filter, pageSize, index == 0 ? 0 : index - 1, out MaxSize);
            MaxPage = MaxSize / UtilHepler.PAGE_SIZE;
            if (MaxSize % UtilHepler.PAGE_SIZE != 0)
                MaxPage += 1;
            if (Shops == null)
                Shops = new List<NetShopMod>();
            foreach (NetShopMod shop in Shops)
            {
                UserMod user = UserBLL.QueryById(shop.UserId);
                if (user != null)
                    shop.User = user;
            }
        }
    }
}